<?php namespace App\Filters;

use App\Models\AttributeProperty;
use App\Models\Region;
use EloquentFilter\ModelFilter;

class HandroomFilter extends ModelFilter
{
    /**
     * @var bool
     * Doing this would allow you to have a company() filter method as well as a companyId()
     */
    protected $drop_id = false;
    /**
     * @var bool
     * Doing this would allow you to have a mobile_phone() filter method instead of mobilePhone()
     */
    protected $camel_cased_methods = false;
    /**
    * Related Models that have ModelFilters as well as the method on the ModelFilter
    * As [relationMethod => [input_key1, input_key2]].
    *
    * @var array
    */
    public $relations = [];

    public function town_id($town_id)
    {
        if (count($town_id)){
            return $this->whereIn('town_id', $town_id);
        }
    }
    public function area($area)
    {
        if ($area > 0){
            $ids = Region::getIds($area);
            return $this->whereIn('area',$ids);
        }
    }
    public function metro_station_id($metro_station_id)
    {
        if(count($metro_station_id)){
            return $this->whereIn('metro_station_id',$metro_station_id);
        }
    }

    public function metro_id($metro_id)
    {
        if ($metro_id > 0){
            return $this->where('metro_id',$metro_id);
        }
    }

    public function community_id($community_id)
    {
        return $this->where('community_id', $community_id);
    }

    public function title($keyword)
    {
        return $this->whereLike('title', $keyword);
    }

    public function wytype($wytype)
    {
        if(count($wytype)){
            return $this->whereIn('wytype', $wytype);
        }
    }

    public function renovation_id($renovation_id)
    {
        if(count($renovation_id)) {
            return $this->whereIn('renovation_id', $renovation_id);
        }
    }

    public function price($price)
    {
        $arrPrice = explode('_',$price);
        if(min($arrPrice)>1){
            return $this->whereBetween('price',[min($arrPrice),max($arrPrice)]);
        }
    }

    public function acreage($acreage)
    {
        $acreage = explode('_',$acreage);
        if(min($acreage)>1){
            return $this->whereBetween('acreage',[min($acreage),max($acreage)]);
        }
    }

    public function room($room)
    {
        if (count($room)){
            // 5室以上
            if (in_array(5,$room)){
                return $this->where(function ($query) use ($room) {
                    $query->whereIn('room', $room)
                        ->orWhere('room', '>', 5);
                });
            }else{
                return $this->whereIn('room',$room);
            }
        }
    }

    public function tags($tags)
    {
        if (count($tags)){
            $ids = AttributeProperty::getPropertyByAttrId($tags,'hand');
            return $this->whereIn('id',$ids);
        }
    }

    public function floor($floor)
    {
        if(count($floor)) {
            return $this->whereIn('floor', $floor);
        }
    }

    public function category_id($direction)
    {
        if(count($direction)) {
            return $this->whereIn('category_id', $direction);
        }
    }

    public function renovation($renovation)
    {
        if(count($renovation)) {
            return $this->whereIn('renovation', $renovation);
        }
    }

    public function year($year)
    {
        $age = explode(',', $year);
        $min = date('Y') - $age[1];
        $max = date('Y') - $age[0];
        return $this->whereBetween('year', [$min, $max]);
    }

}
